package com.hdoit.ares.logical.insu.service.impl;

import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections4.MapUtils;
import org.springframework.stereotype.Component;

import com.hdoit.ares.logical.acc.service.IAwardSettingService;
import com.hdoit.ares.logical.brokerage.model.BrokerageRecord;
import com.hdoit.ares.logical.brokerage.service.IBrokerageRecordService;
import com.hdoit.ares.logical.insu.dao.IZhonganAccidentDao;
import com.hdoit.ares.logical.insu.model.ZhonganAccident;
import com.hdoit.ares.logical.insu.service.IInsuranceService;
import com.hdoit.ares.logical.insu.service.IZhonganAccidentService;
import com.hdoit.ares.logical.policy.service.IListPolicyService;
import com.hdoit.ares.logical.tp.model.ZhonganCallback;
import com.hdoit.ares.selfcommon.enums.acc.InsuranceType;
import com.hdoit.ares.selfcommon.utils.ExpressionUtil;
import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.xiaoleilu.hutool.json.JSONUtil;
import com.xiaoleilu.hutool.log.dialect.log4j2.Log4j2LogFactory;
/**
 * @author      caochengmeng
 * @email       
 * @createtime  2018年01月16日 09:00:41
 * @comment
 */
@Component
public class ZhonganAccidentServiceImpl extends SuperService<ZhonganAccident> implements IZhonganAccidentService{
	
	private IListPolicyService listPolicyService;
	
	public IListPolicyService getListPolicyService() {
		return listPolicyService;
	}
	@Resource
	public void setListPolicyService(IListPolicyService listPolicyService) {
		this.listPolicyService = listPolicyService;
	}

	private IInsuranceService insuranceService;
	
	public IInsuranceService getInsuranceService() {
		return insuranceService;
	}
	@Resource
	public void setInsuranceService(IInsuranceService insuranceService) {
		this.insuranceService = insuranceService;
	}

	private IAwardSettingService awardSettingService;
	
	public IAwardSettingService getAwardSettingService() {
		return awardSettingService;
	}
	@Resource
	public void setAwardSettingService(IAwardSettingService awardSettingService) {
		this.awardSettingService = awardSettingService;
	}

	private IZhonganAccidentDao zhonganAccidentDao;
	public IZhonganAccidentDao getZhonganAccidentDao() {
		return zhonganAccidentDao;
	}
	@Resource
	public void setZhonganAccidentDao(IZhonganAccidentDao zhonganAccidentDao) {
		this.zhonganAccidentDao = zhonganAccidentDao;
	}
	
	private IBrokerageRecordService brokerageRecordService;
	public IBrokerageRecordService getBrokerageRecordService() {
		return brokerageRecordService;
	}
	@Resource
	public void setBrokerageRecordService(IBrokerageRecordService brokerageRecordService) {
		this.brokerageRecordService = brokerageRecordService;
	}
	
	@Override
	public ZhonganAccident queryModelByPolicyNo(String policyNo) {
		return zhonganAccidentDao.queryModelByPolicyNo(policyNo);
	}
	@Override
	public String zhonganAccidentCallback(ZhonganCallback zhonganCallback) throws RuntimeException {
		ZhonganAccident zhonganAccident = new ZhonganAccident();
		zhonganAccident.setId(CommonUtils.getKeyId());
		zhonganAccident.setModelStatus(ModelStatus.add);
		zhonganAccident.setRequestDate(DateUtils.getCurrent());

		String bizContent = zhonganCallback.getBizContent();	
		Map<String, Object> bizContentMap = JSONUtil.parseObj(bizContent);
		String userID = MapUtils.getString(bizContentMap, "userID");
		String[] userIDed = userID.split("_");
		String customerId = userIDed[0];
		String prodId = userIDed[1];
		
		zhonganAccident.setUserId(customerId);
		zhonganAccident.setProductId(prodId);
		
		zhonganAccident.setOldPolicyNo(zhonganCallback.getOldPolicyNo());
		zhonganAccident.setPolicyNo(zhonganCallback.getPolicyNo());
		zhonganAccident.setSumInsured(zhonganCallback.getSumInsured());
		zhonganAccident.setPremium(zhonganCallback.getPremium());
		
		try {
			Date effectiveDate = DateUtils.convertString2Date(zhonganCallback.getEffectiveDate());
			zhonganAccident.setEffectiveDate(effectiveDate);
		} catch (ParseException e) {
			Log4j2LogFactory.get().error("zhongan expiryDate set error"+e);
			e.printStackTrace();
		}
	
		String expiryDate1 = zhonganCallback.getExpiryDate();
		try {
			Date expiryDate2 = DateUtils.convertString2Date(expiryDate1);
			Date expiryDate3 = DateUtils.addDay(1, expiryDate2);
			Calendar expiryDatecalendar = Calendar.getInstance();
			expiryDatecalendar.setTime(expiryDate3);
			Date date=expiryDatecalendar.getTime();
			System.err.println(date+"date");
			zhonganAccident.setExpiryDate(date);
		}catch (ParseException e) {
			Log4j2LogFactory.get().error("zhongan expiryDate set error"+e);
			e.printStackTrace();
		}
		zhonganAccident.setProductName(zhonganCallback.getProductName());
		zhonganAccident.setPromoteCode(zhonganCallback.getPromoteCode());
		zhonganAccident.setPromoteName(zhonganCallback.getPromoteName());
		zhonganAccident.setPromoteFee(zhonganCallback.getPromoteFee());
	
		if (!applyChange(zhonganAccident)) {
			throw new RuntimeException("zhonganAccident save error");
		}
		
		if(!listPolicyService.setZhonganAccidentPolicy(zhonganAccident)){
			throw new RuntimeException("zhonganAccident set error");
		}
		
		BrokerageRecord brokerageRecord = new BrokerageRecord();
		Double proportion = insuranceService.queryModel(zhonganAccident.getProductId()).getTotalProportion();
		String premium = zhonganAccident.getPremium();
		brokerageRecord.setId(CommonUtils.getKeyId());
		brokerageRecord.setBrokerage(Double.valueOf(premium));
		
		double tax = ExpressionUtil.stringToDouble((awardSettingService.queryModelByHC("tax", InsuranceType.NotCar).getExpression()),0.00);
		
		Double taxPercent = new BigDecimal(premium).multiply(new BigDecimal(String.valueOf(proportion))).multiply(new BigDecimal(String.valueOf(tax))).setScale(2, BigDecimal.ROUND_DOWN).doubleValue();
		brokerageRecord.setTaxPercent(taxPercent);
		brokerageRecord.setUserId(zhonganAccident.getUserId());
		brokerageRecord.setRecordDate(zhonganAccident.getRequestDate());
		brokerageRecord.setCurrency("元");
		brokerageRecord.setCompanyName(zhonganAccident.getProductName());

		// 意外险犹豫期
		Calendar nowcalendar = Calendar.getInstance();
		Calendar startDatecalendar = Calendar.getInstance();
		nowcalendar.setTime(DateUtils.getCurrent());
		startDatecalendar.setTime(zhonganAccident.getEffectiveDate());
		int day = DateUtils.getDaysBetween(nowcalendar, startDatecalendar);
		brokerageRecord.setHesitateDate(day);

/*		// 订单号保单号
		brokerageRecord.setOrderSn(zhonganAccident.getPolicyNo());*/
		brokerageRecord.setPolicyNo(zhonganAccident.getPolicyNo());

		brokerageRecord.setProductId(zhonganAccident.getPromoteCode());
		brokerageRecord.setProportion(proportion);
		brokerageRecord.setRecordDate(DateUtils.getCurrent());
		brokerageRecord.setModelStatus(ModelStatus.add);
		if (!brokerageRecordService.applyChange(brokerageRecord, InsuranceType.NotCar)) {
			throw new RuntimeException("brokerageRecord save error");
		}
		return "ok";
	}

}